package com.itheima.goods.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.goods.mapper.BrandMapper;
import com.itheima.goods.service.BrandService;
import com.itheima.goods.utils.PageInfo2PageResultUtil;
import com.itheima.pojo.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @author TSV
 * @description 品牌接口实现类
 * @date 2021/8/16
 */
@Service("brandService")
public class BrandServiceImpl implements BrandService {


    @Autowired
    private BrandMapper brandMapper;

    /**
     * 功能描述 查询所有品牌
     *
     * @param
     * @return com.itheima.entity.Result
     * @auther TSV
     * @date 2021/8/16
     */
    @Override
    public Result findAll() {
        // 调用Mapper接口方法查询所有品牌
        List<Brand> list = this.brandMapper.findAll();
        // 返回结果
        return new Result(StatusCode.OK, true, "", list);
    }

    /**
     * 功能描述 添加品牌
     *
     * @param brand
     * @return java.lang.Boolean
     * @auther TSV
     * @date 2021/8/16
     */
    @Override
    public Boolean add(Brand brand) {
        return this.brandMapper.add(brand) > 0 ? true : false;
    }
    /**
     *功能描述 分页查询
     * @auther TSV
     * @date 2021/8/16
     * @param page
     * @param size
     * @return com.itheima.entity.Result
     */
    @Override
    public Result findPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        List<Brand> list = this.brandMapper.findAll();
        PageResult pageResult = new PageInfo2PageResultUtil<Brand>().pageInfo2PageResult(new PageInfo<>(list));
        return new Result(StatusCode.OK, true, "", pageResult);
    }
    /**
     *功能描述 根据id查询品牌
     * @auther TSV
     * @date 2021/8/16
     * @param id
     * @return com.itheima.entity.Result
     */
    @Override
    public Result findById(Integer id) {
        Brand brand = this.brandMapper.findById(id);
        return new Result(StatusCode.OK, true, "", brand);
    }
    /**
     *功能描述 根据id修改品牌
     * @auther TSV
     * @date 2021/8/16
     * @param id
     * @param brand
     * @return com.itheima.entity.Result
     */
    @Override
    public Boolean update(Integer id, Brand brand) {
        brand.setId(id);
        return this.brandMapper.update(brand) > 0 ? true : false;
    }
    /**
     *功能描述 删除品牌
     * @auther TSV
     * @date 2021/8/16
     * @param id
     * @return java.lang.Boolean
     */
    @Override
    public Boolean delete(Integer id) {
        return this.brandMapper.delete(id)> 0 ? true : false;
    }
}
